package com.futo.platformplayer.receivers

import android.content.ActivityNotFoundException
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInstaller
import android.os.Build
import com.futo.platformplayer.R
import com.futo.platformplayer.constructs.Event1
import com.futo.platformplayer.logging.Logger


class InstallReceiver : BroadcastReceiver() {
    private val TAG = "InstallReceiver"

    companion object {
        val onReceiveResult = Event1<String?>();
    }

    override fun onReceive(context: Context, intent: Intent) {
        val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, -1);
        Logger.i(TAG, "Received status $status.");

        when (status) {
            PackageInstaller.STATUS_PENDING_USER_ACTION -> {
                val activityIntent: Intent? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                    intent.getParcelableExtra(Intent.EXTRA_INTENT, Intent::class.java)
                } else {
                    @Suppress("DEPRECATION")
                    intent.getParcelableExtra(Intent.EXTRA_INTENT)
                }

                if (activityIntent == null) {
                    Logger.w(TAG, "Received STATUS_PENDING_USER_ACTION and activity intent is null.")
                    onReceiveResult.emit(context.getString(R.string.install_failed_device_installer_broken))
                    return;
                }

                activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

                try {
                    context.startActivity(activityIntent)
                } catch (e: ActivityNotFoundException) {
                    Logger.e(TAG, "System installer cannot handle CONFIRM_INSTALL intent. ROM is broken; falling back / reporting error.", e)
                    onReceiveResult.emit(context.getString(R.string.install_failed_device_installer_broken))
                }
            }
            PackageInstaller.STATUS_SUCCESS -> onReceiveResult.emit(null);
            PackageInstaller.STATUS_FAILURE -> onReceiveResult.emit(context.getString(R.string.general_failure));
            PackageInstaller.STATUS_FAILURE_ABORTED -> onReceiveResult.emit(context.getString(R.string.aborted));
            PackageInstaller.STATUS_FAILURE_BLOCKED -> onReceiveResult.emit(context.getString(R.string.blocked));
            PackageInstaller.STATUS_FAILURE_CONFLICT -> onReceiveResult.emit(context.getString(R.string.conflict));
            PackageInstaller.STATUS_FAILURE_INCOMPATIBLE -> onReceiveResult.emit(context.getString(R.string.incompatible));
            PackageInstaller.STATUS_FAILURE_INVALID -> onReceiveResult.emit(context.getString(R.string.invalid));
            PackageInstaller.STATUS_FAILURE_STORAGE -> onReceiveResult.emit(context.getString(R.string.not_enough_storage));
            else -> {
                val msg = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
                Logger.w(TAG, "Received unknown install status $status, message=$msg")
                onReceiveResult.emit(msg)
            }
        }
    }
}